package cn.community.care.module.system.service.activities;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.module.system.controller.admin.activities.vo.*;
import cn.community.care.module.system.dal.dataobject.activities.ActivitiesDO;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;

import cn.community.care.module.system.dal.mysql.activities.ActivitiesMapper;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.community.care.module.system.enums.ErrorCodeConstants.*;

/**
 * 活动信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ActivitiesServiceImpl implements ActivitiesService {

    @Resource
    private ActivitiesMapper activitiesMapper;

    @Override
    public Long createActivities(ActivitiesSaveReqVO createReqVO) {
        // 插入
        ActivitiesDO activities = BeanUtils.toBean(createReqVO, ActivitiesDO.class);
        activitiesMapper.insert(activities);
        // 返回
        return activities.getId();
    }

    @Override
    public void updateActivities(ActivitiesSaveReqVO updateReqVO) {
        // 校验存在
        validateActivitiesExists(updateReqVO.getId());
        // 更新
        ActivitiesDO updateObj = BeanUtils.toBean(updateReqVO, ActivitiesDO.class);
        activitiesMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivities(Long id) {
        // 校验存在
        validateActivitiesExists(id);
        // 删除
        activitiesMapper.deleteById(id);
    }

    private void validateActivitiesExists(Long id) {
        if (activitiesMapper.selectById(id) == null) {
//            throw exception(ACTIVITIES_NOT_EXISTS);
        }
    }

    @Override
    public ActivitiesDO getActivities(Long id) {
        return activitiesMapper.selectById(id);
    }

    @Override
    public PageResult<ActivitiesDO> getActivitiesPage(ActivitiesPageReqVO pageReqVO) {
        return activitiesMapper.selectPage(pageReqVO);
    }

}